﻿using MSAcademic.v2.Infrastructure.Tag;
using MSAcademic.v2.Web.Areas.Admin.Models.Tag;
using StructureMap;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MSAcademic.v2.Web.Areas.Admin.Controllers
{
    public partial class TagController : Controller
    {
        public ActionResult TagList(TagListModel model)
        {
            if (!model.CanAccess) { return RedirectToRoute("Index"); }

            ViewData["CurrentView"] = "Tags";
            return View(model);
        }

        [HttpPost]
        public ActionResult TagList(TagListModel model, bool? post)
        {
            if(!model.CanAccess) { return RedirectToRoute("Index"); }

            if (!string.IsNullOrWhiteSpace(model.NewTagName))
            {
                _tagCommand.Create(model.NewTagName);
            }

            return RedirectToRoute("TagIndexAdmin");
        }

        public ActionResult EditTag(EditTagModel model)
        {
            if (!model.CanAccess) { return RedirectToRoute("Index"); }

            return PartialView(model);
        }

        [HttpPost]
        public ActionResult EditTag(EditTagModel model, bool? post)
        {
            if(!model.CanAccess) { return RedirectToRoute("Index"); }
            //TODO: validacija
            _tagCommand.Edit(model.TagId, model.Tag.Name);

            return RedirectToRoute("TagIndexAdmin");
        }

        public ActionResult DeleteTag(int tagId)
        {
            //TODO: UA
            _tagCommand.Delete(tagId);

            return RedirectToRoute("TagIndexAdmin");
        }

    }

    public partial class TagController
    {
        private ITagCommand _tagCommand;

        public TagController(
            ITagCommand tagCommand)
        {
            _tagCommand = tagCommand;
        }
    }
}
